using System;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;

namespace FireStarterModeller.UI
{
    public class SolutionControl : UserControlWin
    {
        private readonly IControlFactory _controlFactory;
        private readonly IPanelInfo _panelInfo;
        public IButtonGroupControl ButtonGroupControl { get; private set; }
        public event EventHandler<BOEventArgs<DMSolution>> SolutionSaved;


        public SolutionControl()
        {
            _controlFactory = GlobalUIRegistry.ControlFactory;
            PanelBuilder panelBuilder = new PanelBuilder(_controlFactory);
            _panelInfo = panelBuilder.BuildPanelForForm(ClassDef.Get<DMSolution>().UIDefCol["default"].UIForm);
            CreateButtonGroupControl();
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(PanelInfo.Panel, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(this.ButtonGroupControl, BorderLayoutManager.Position.South);
            PanelInfo.BusinessObject = new DMSolution();

        }

        private void CreateButtonGroupControl()
        {
            ButtonGroupControl = _controlFactory.CreateButtonGroupControl();
            ButtonGroupControl.AddButton("Cancel", "Cancel", Cancel_ClickHandler);
            ButtonGroupControl.AddButton("Save", "Save", Save_ClickHandler);
            ButtonGroupControl.AddButton("Delete"); //, "Delete", Delete_ClickHandler);
        }

        private void Cancel_ClickHandler(object sender, EventArgs e)
        {
            this.PanelInfo.BusinessObject = null;
            FireSolutionSaved();
        }

        public DMSolution DMSolution
        {
            get { return (DMSolution) _panelInfo.BusinessObject; }
        }

        public IPanelInfo PanelInfo
        {
            get { return _panelInfo; }
        }

        private void Save_ClickHandler(object sender, EventArgs e)
        {
            try
            {
                DMSolution solution = (DMSolution) this.PanelInfo.BusinessObject;
                if (solution == null) return;
                this.PanelInfo.ApplyChangesToBusinessObject();
                if (solution.DefaultAssembly == null)
                {
                    solution.DefaultAssembly = solution.CreateAssembly("DefaultModule");
                }
                SolutionCreator solutionCreator = new SolutionCreator();
                solutionCreator.UpdateNewSolution(solution);
                solution.Save();
                FireSolutionSaved();
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        private void FireSolutionSaved()
        {
            if (this.SolutionSaved != null)
            {
                this.SolutionSaved(this, new BOEventArgs<DMSolution>(this.DMSolution));
            }
        }
    }
}